74.2. Externalize the Configuration of SpringApplication
74.2. 外部化SpringApplication配置
SpringApplication已经被属性化(主要是setters),所以你可以在创建应用时使用它的Java API修改其行为,或者使用以spring.main.*为key的属性来外部化这些配置。比如,在application.properties中可能会有以下内容:
spring.main.web-environment=false
spring.main.banner-mode=off
这样,Spring Boot在启动时将不会显示banner,并且该应用也不是一个web应用。
注 以上示例也展示在属性名中使用下划线(_)和中划线(-)的灵活绑定。
外部配置定义的属性会覆盖创建ApplicationContext时通过Java API指定的值,让我们看如下应用:
new SpringApplicationBuilder()
    .bannerMode(Banner.Mode.OFF)
    .sources(demo.MyApp.class)
    .run(args);
并使用以下配置:
spring.main.sources=com.acme.Config,com.acme.ExtraConfig
spring.main.banner-mode=console
实际的应用将显示banner(被配置覆盖),并为ApplicationContext指定3个sources,依次为:demo.MyApp,com.acme.Config,com.acme.ExtraConfig。